/* UTF-8 with BOM */
#include "menu.h"
#include "oled.h"
#include "fingerprint.h"
#include "password.h"
#include "rfid.h"
#include "motor.h"
#include "power_mgmt.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>

// �˵���ʱ�ص�����
static void (*timeoutCallback)(void) = NULL;
static uint32_t lastOperationTime = 0;

// �˵�����
static MenuItem mainMenu[] = {
    {"����", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0},
    {"�û�����", MENU_ITEM_SUBMENU, NULL, NULL, NULL, 0},
    {"ϵͳ����", MENU_ITEM_SUBMENU, NULL, NULL, NULL, 0},
    {"����", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0}
};

static MenuItem userMenu[] = {
    {"����ָ��", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0},
    {"ɾ��ָ��", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0},
    {"��������", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0},
    {"ɾ������", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0},
    {"���ӿ�Ƭ", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0},
    {"ɾ����Ƭ", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0}
};

static MenuItem settingsMenu[] = {
    {"ϵͳʱ��", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0},
    {"ʡ������", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0},
    {"ϵͳ��Ϣ", MENU_ITEM_FUNCTION, NULL, NULL, NULL, 0}
};

// ��ǰ�˵�״̬
static MenuItem *currentMenu = mainMenu;
static uint8_t currentIndex = 0;
static uint8_t menuDepth = 0;

// �˵���ʼ��
SystemStatus MENU_Init(void)
{
    // ��ʼ���˵��ĸ��ӹ�ϵ
    mainMenu[1].submenu = userMenu;
    mainMenu[1].submenu_count = sizeof(userMenu) / sizeof(userMenu[0]);
    mainMenu[2].submenu = settingsMenu;
    mainMenu[2].submenu_count = sizeof(settingsMenu) / sizeof(settingsMenu[0]);
    
    for (uint8_t i = 0; i < sizeof(userMenu) / sizeof(userMenu[0]); i++) {
        userMenu[i].parent = &mainMenu[1];
    }
    
    for (uint8_t i = 0; i < sizeof(settingsMenu) / sizeof(settingsMenu[0]); i++) {
        settingsMenu[i].parent = &mainMenu[2];
    }
    
    // ��ʼ����ʾ
    MENU_Display();
    
    return SYSTEM_OK;
}

// ��ʾ��ǰ�˵�
void MENU_Display(void)
{
    OLED_Clear();
    
    // ��ʾ�˵�����
    if (menuDepth > 0 && currentMenu[0].parent != NULL) {
        OLED_ShowChinese(0, 0, currentMenu[0].parent->text);
    } else {
        OLED_ShowChinese(0, 0, "���˵�");
    }
    
    // ��ʾ�˵���
    for (uint8_t i = 0; i < 3 && i < currentMenu[currentIndex].submenu_count; i++) {
        if (i == currentIndex) {
            OLED_ShowString(0, i + 2, "> ");
        }
        OLED_ShowChinese(16, i + 2, currentMenu[i].text);
    }
    
    // ���²���ʱ��
    lastOperationTime = 0;  // TODO: ��ȡϵͳʱ��
}

// �˵����� - ��
void MENU_Up(void)
{
    if (currentIndex > 0) {
        currentIndex--;
        MENU_Display();
    }
}

// �˵����� - ��
void MENU_Down(void)
{
    if (currentIndex < currentMenu[currentIndex].submenu_count - 1) {
        currentIndex++;
        MENU_Display();
    }
}

// �˵����� - ȷ��
void MENU_Enter(void)
{
    MenuItem *item = &currentMenu[currentIndex];
    
    switch (item->type) {
        case MENU_ITEM_SUBMENU:
            if (item->submenu != NULL) {
                currentMenu = item->submenu;
                currentIndex = 0;
                menuDepth++;
                MENU_Display();
            }
            break;
            
        case MENU_ITEM_FUNCTION:
            if (item->callback != NULL) {
                item->callback();
            }
            break;
            
        default:
            break;
    }
}

// �˵����� - ����
void MENU_Back(void)
{
    if (menuDepth > 0 && currentMenu[0].parent != NULL) {
        currentMenu = currentMenu[0].parent;
        currentIndex = 0;
        menuDepth--;
        MENU_Display();
    }
}

// ��ȡ��ǰ�˵���
MenuItem* MENU_GetCurrentItem(void)
{
    return &currentMenu[currentIndex];
}

// ���ò˵���ʱ�ص�
void MENU_SetTimeoutCallback(void (*callback)(void))
{
    timeoutCallback = callback;
}

// �˵���ʱ���
void MENU_TimerUpdate(void)
{
    uint32_t currentTime = 0;  // TODO: ��ȡϵͳʱ��
    
    if (timeoutCallback != NULL &&
        currentTime - lastOperationTime > MENU_TIMEOUT) {
        timeoutCallback();
    }
} 